package handler

import (
	"context"
	"encoding/json"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/global"
	"mxshop_srvs/goods_srv/model"
	"mxshop_srvs/goods_srv/proto"
)

// //商品分类
func (g *GoodsServer) GetAllCategorysList(ctx context.Context, e *emptypb.Empty) (*proto.CategoryListResponse, error) {

	var categoryListResponse proto.CategoryListResponse
	var categoryList []model.Category

	result := global.DB.Preload("SubCategory.SubCategory").Find(&categoryList)
	categoryJson, err := json.Marshal(&categoryList)
	if err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}
	categoryListResponse.JsonData = string(categoryJson)
	if result.Error != nil {
		return nil, status.Error(codes.Internal, result.Error.Error())
	}
	categoryListResponse.Total = int32(result.RowsAffected)

	for _, category := range categoryList {
		categoryInfoResponse := proto.CategoryInfoResponse{
			Id:             category.BaseModel.ID,
			Name:           category.Name,
			ParentCategory: category.ParentCategoryID,
			Level:          category.Level,
			IsTab:          false,
		}
		categoryListResponse.Data = append(categoryListResponse.Data, &categoryInfoResponse)
	}

	return &categoryListResponse, nil
}

// //获取子分类
func (g *GoodsServer) GetSubCategory(ctx context.Context, req *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {

	var subCategoryListResponse proto.SubCategoryListResponse
	var category model.Category
	if result := global.DB.Find(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "分类不存在")
	}
	categoryInfoResponse := proto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		ParentCategory: category.ParentCategoryID,
		Level:          category.Level,
		IsTab:          category.IsTab,
	}
	subCategoryListResponse.Info = &categoryInfoResponse
	var subCategory []model.Category
	if result := global.DB.Where("ParentCategoryID", req.Id).Find(&subCategory); result.Error != nil {
		return nil, status.Error(codes.Internal, result.Error.Error())
	}
	for _, category := range subCategory {
		categoryInfoResponse := proto.CategoryInfoResponse{
			Id:             category.ID,
			Name:           category.Name,
			ParentCategory: category.ParentCategoryID,
			Level:          category.Level,
			IsTab:          category.IsTab,
		}
		subCategoryListResponse.SubCategorys = append(subCategoryListResponse.SubCategorys, &categoryInfoResponse)
	}
	return &subCategoryListResponse, nil
}

func (g *GoodsServer) CreateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {

	category := model.Category{
		Name:             req.Name,
		ParentCategoryID: req.ParentCategory,
		Level:            req.Level,
		IsTab:            req.IsTab,
	}

	if result := global.DB.Save(&category); result.Error != nil {
		return nil, status.Error(codes.Internal, result.Error.Error())
	}
	return &proto.CategoryInfoResponse{Id: category.ID}, nil
}
func (g *GoodsServer) DeleteCategory(ctx context.Context, req *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {

	if result := global.DB.Delete(&model.Category{}, req.Id); result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "分类不存在")
	}
	return &emptypb.Empty{}, nil

}
func (g *GoodsServer) UpdateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*emptypb.Empty, error) {

	var category model.Category
	if result := global.DB.Find(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "分类不存在")
	}
	if req.Name != "" {
		category.Name = req.Name
	}
	if req.Level != 0 {
		category.Level = req.Level
	}
	if req.ParentCategory != 0 {
		category.ParentCategoryID = req.ParentCategory
	}
	if result := global.DB.Save(category); result.Error != nil {
		return nil, status.Error(codes.Internal, result.Error.Error())
	}
	return &emptypb.Empty{}, nil
}
